/**
 *
 * @author Doan Tuan Anh
 */

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;

public class Piranha extends Sprite {
    
    private static final int TOTAL_FRAME = 5;
    boolean right;
    private int myY;
    int WIDTH, HEIGHT;
    private int leftBound, rightBound;

    public Piranha(boolean right, int initialY) throws Exception {
    
       super(Image.createImage("/piranha.png"), Image.createImage("/piranha.png").getWidth() / TOTAL_FRAME, Image.createImage("/piranha.png").getHeight());

       defineReferencePixel(super.getWidth() / 2, super.getHeight() / 2);
       
       WIDTH = super.getWidth();
       HEIGHT = super.getHeight();
       
       myY = initialY;
       this.right = right;
       
       if(!right) setTransform(TRANS_MIRROR);
   
       setVisible(false);
       
    }
    void checkCollision(Piranha piranha)
    {

        if(collidesWith(piranha, true)) 
        {
            if(getRefPixelX() >= rightBound - 40 || getRefPixelX() <= leftBound + 40)
            {
                setVisible(false);
                return;
            }

            if(right != piranha.right)
            {
                changeDirection(4);
                piranha.changeDirection(4);
            }
            else
            {
                if(right)
                {
                    if(getRefPixelX() > piranha.getRefPixelX()) setVisible(false);
                    else piranha.setVisible(false);
                }
                else
                {
                    if(getRefPixelX() > piranha.getRefPixelX()) piranha.setVisible(false);
                    else setVisible(false);
                }
            }
        }
    }
      
   void changeDirection(int distX)
    {
        right = !right;

        if(!right)
        {
            setTransform(TRANS_MIRROR);
            move(distX, 0);
        }
        else
        {
            setTransform(TRANS_NONE);
            move(-distX, 0);
        }
    }

  synchronized boolean go(boolean right, int posY) {
    boolean retVal = false;
    if(!isVisible()) {
      retVal = true;
      myY = posY;
      this.right = right;
      
      if(!right) setTransform(TRANS_MIRROR);
      else setTransform(TRANS_NONE);
      
      setVisible(true);
      if(right) {
	setRefPixelPosition(rightBound, myY);
	move(-1, 0);
      } else { //right
	setRefPixelPosition(leftBound, myY);
	move(1, 0);
      }
    } else {
    }
    return(retVal);
  }

  void reset() {
    setVisible(false);
  }

  synchronized int advance(Duck duck, int tickCount,
	      int currentLeftBound, int currentRightBound) {
    int retVal = 0;
    leftBound = currentLeftBound;
    rightBound = currentRightBound;
    
    if((getRefPixelX() - WIDTH >= rightBound) && (!right)) {
      setVisible(false);
    } 
    if((getRefPixelX() + WIDTH <= leftBound) && right) {
      setVisible(false);
    } 
    if(isVisible()) {
       nextFrame();
      if(right) {
	move(-3, 0);
      } else {
	move(3, 0);
      }
    }
    return(retVal);
  }
}
